package ua.edu.nuos.jeetraining2012.automator.translator.dao;

import ua.edu.nuos.jeetraining2012.automator.translator.vo.Bpmn2Entity;
import ua.edu.nuos.jeetraining2012.automator.translator.vo.InhouseEntity;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import java.nio.file.Path;

/**
 * @author Timur Yarosh
 *         Date: 03.06.13
 *         Time: 21:02
 */
public class TranslatorDao {

    public Bpmn2Entity loadBpmn2Entity(Path xml) throws JAXBException {
        JAXBContext context = JAXBContext.newInstance(Bpmn2Entity.class);
        return (Bpmn2Entity) context.createUnmarshaller().unmarshal(xml.toFile());
    }

    public void storeBpmn2Entity(Bpmn2Entity entity, Path storePath) throws JAXBException {
        JAXBContext context = JAXBContext.newInstance(Bpmn2Entity.class);
        Marshaller marshaller = context.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(entity, storePath.toFile());
    }

    public InhouseEntity loadInhouseEntity(Path xml) throws JAXBException {
        JAXBContext context = JAXBContext.newInstance(InhouseEntity.class);
        return (InhouseEntity) context.createUnmarshaller().unmarshal(xml.toFile());
    }

    public void storeInhouseEntity(InhouseEntity entity, Path storePath) throws JAXBException {
        JAXBContext context = JAXBContext.newInstance(InhouseEntity.class);
        Marshaller marshaller = context.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(entity, storePath.toFile());
    }
}
